<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="tp5.1," />










<meta name="description" content="参考链接 https:&#x2F;&#x2F;www.kancloud.cn&#x2F;manual&#x2F;thinkphp5_1&#x2F;354135 文档地址 http:&#x2F;&#x2F;www.runoob.com&#x2F;mongodb&#x2F;mongodb-tutorial.html 安装扩展     1composer require topthink&#x2F;think-mongo">
<meta name="keywords" content="tp5.1">
<meta property="og:type" content="article">
<meta property="og:title" content="tp5.1 for mongodb">
<meta property="og:url" content="http:&#x2F;&#x2F;yoursite.com&#x2F;2019&#x2F;03&#x2F;27&#x2F;tp51-mongodb&#x2F;index.html">
<meta property="og:site_name" content="jZed Blog">
<meta property="og:description" content="参考链接 https:&#x2F;&#x2F;www.kancloud.cn&#x2F;manual&#x2F;thinkphp5_1&#x2F;354135 文档地址 http:&#x2F;&#x2F;www.runoob.com&#x2F;mongodb&#x2F;mongodb-tutorial.html 安装扩展     1composer require topthink&#x2F;think-mongo">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-11-13T07:08:19.297Z">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/03/27/tp51-mongodb/"/>





  <title>tp5.1 for mongodb | jZed Blog</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?63267aed0a7cf33839963a477337b7f0";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">jZed Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-/home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-tp5.1" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/03/27/tp51-mongodb/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="jZed">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="jZed Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">tp5.1 for mongodb</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-03-27T14:43:42+08:00">
                2019-03-27
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/tp5-1/" itemprop="url" rel="index">
                    <span itemprop="name">tp5.1</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>阅读数
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><strong>参考链接</strong> <a href="https://www.kancloud.cn/manual/thinkphp5_1/354135" target="_blank" rel="noopener">https://www.kancloud.cn/manual/thinkphp5_1/354135</a></p>
<p><strong>文档地址</strong> <a href="http://www.runoob.com/mongodb/mongodb-tutorial.html" target="_blank" rel="noopener">http://www.runoob.com/mongodb/mongodb-tutorial.html</a></p>
<p><strong>安装扩展</strong>    </p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">composer require topthink/think-mongo</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<p><strong>配置参数</strong></p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">            <span class="string">'type'</span>            =&gt; <span class="string">'\think\mongo\Connection'</span>,</span><br><span class="line">​</span><br><span class="line">            <span class="string">'query'</span>           =&gt; <span class="string">'\think\mongo\Query'</span>,</span><br><span class="line">            <span class="comment">// 服务器地址</span></span><br><span class="line">            <span class="string">'hostname'</span>        =&gt; <span class="string">'127.0.0.1'</span>,</span><br><span class="line">            <span class="comment">// 数据库名</span></span><br><span class="line">            <span class="string">'database'</span>        =&gt; <span class="string">'jZed'</span>,</span><br><span class="line">            <span class="comment">// 用户名</span></span><br><span class="line">            <span class="string">'username'</span>        =&gt; <span class="string">'root'</span>,</span><br><span class="line">            <span class="comment">// 密码</span></span><br><span class="line">            <span class="string">'password'</span>        =&gt; <span class="string">'root'</span>,</span><br><span class="line">            <span class="comment">// 端口</span></span><br><span class="line">            <span class="string">'hostport'</span>        =&gt; <span class="string">'3717'</span>,</span><br><span class="line">            <span class="comment">// 强制把_id转换为id</span></span><br><span class="line">            <span class="string">'pk_convert_id'</span>  =&gt; <span class="keyword">true</span>,</span><br></pre></td></tr></table></figure>
<p><strong>连接</strong></p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Db::connect($config)</span><br></pre></td></tr></table></figure>
<p><strong>简单使用</strong></p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//添加</span></span><br><span class="line">Mongodb::mongodb_db()-&gt;table(<span class="string">'test'</span>)-&gt;insert([<span class="string">'x'</span> =&gt; <span class="number">1</span>, <span class="string">'name'</span>=&gt;<span class="string">'菜鸟教程'</span>, <span class="string">'url'</span> =&gt; <span class="string">'http://www.runoob.com'</span>]);</span><br><span class="line">​</span><br><span class="line"><span class="comment">//添加并返回主键id</span></span><br><span class="line">$insertGetId =  Mongodb::mongodb_db()-&gt;table(<span class="string">'test'</span>)-&gt;insertGetId([<span class="string">'x'</span> =&gt; <span class="number">1</span>, <span class="string">'name'</span>=&gt;<span class="string">'菜鸟教程'</span>, <span class="string">'url'</span> =&gt; <span class="string">'http://www.runoob.com'</span>]);</span><br><span class="line">​</span><br><span class="line">​</span><br><span class="line"><span class="comment">//查询 单条</span></span><br><span class="line">$find = Mongodb::mongodb_db()-&gt;table(<span class="string">'test'</span>)-&gt;where([<span class="string">'x'</span>=&gt;<span class="number">1</span>])-&gt;find();</span><br><span class="line">​</span><br><span class="line">$find = Mongodb::mongodb_db()-&gt;table(<span class="string">'test'</span>)-&gt;find(<span class="string">'5ca1abd9365760023d2ed254'</span>);</span><br><span class="line">​</span><br><span class="line"><span class="comment">//查询多条</span></span><br><span class="line">​</span><br><span class="line">$select_all =  Mongodb::mongodb_db()-&gt;table(<span class="string">'test'</span>)-&gt;select();</span><br><span class="line">​</span><br><span class="line">$select = Mongodb::mongodb_db()-&gt;table(<span class="string">'test'</span>)-&gt;where([<span class="string">'x'</span>=&gt;<span class="number">1</span>])-&gt;select();</span><br><span class="line">​</span><br><span class="line"><span class="comment">//更新</span></span><br><span class="line">Mongodb::mongodb_db()-&gt;table(<span class="string">'test'</span>)-&gt;where(<span class="string">'id'</span>,<span class="string">'5ca1ab50365760023d2ed253'</span>)-&gt;update([<span class="string">'x'</span>=&gt;<span class="number">2</span>,<span class="string">'name'</span>=&gt;<span class="string">'ceshi'</span>,<span class="string">'url'</span>=&gt;<span class="string">'http://www.baidu.com'</span>]);</span><br><span class="line">​</span><br><span class="line"><span class="comment">//删除</span></span><br><span class="line">Mongodb::mongodb_db()-&gt;table(<span class="string">'test'</span>)-&gt;where(<span class="string">'id'</span>,<span class="string">'5ca1aaee365760023c26a7e1'</span>)-&gt;delete();</span><br></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/tp5-1/" rel="tag"># tp5.1</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/03/27/tp51-redis/" rel="next" title="tp5.1 for redis">
                <i class="fa fa-chevron-left"></i> tp5.1 for redis
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/10/29/Ubuntu1804-for-mysql/" rel="prev" title="Ubuntu18.04 for MySQL">
                Ubuntu18.04 for MySQL <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">jZed</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">13</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">5</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">5</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">jZed</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>访问人数
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>总访问量
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
